package com.ocean.web.controller.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ocean.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.metadata.domain.OMetadataElement;
import com.ocean.metadata.service.IOMetadataElementService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 元数据要素Controller
 *
 * @author ocean
 * @date 2023-01-29
 */
@Controller
@RequestMapping("/metadata/metadataElement")
public class OMetadataElementController extends BaseController
{
    private String prefix = "metadata/metadataElement";

    @Autowired
    private IOMetadataElementService oMetadataElementService;

    @RequiresPermissions("metadata:metadataElement:view")
    @GetMapping()
    public String metadataElement()
    {
        return prefix + "/metadataElement";
    }

    /**
     * 查询元数据要素列表
     */
    @RequiresPermissions("metadata:metadataElement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OMetadataElement oMetadataElement)
    {
        List<OMetadataElement> result = new ArrayList<>();
        if (!StringUtils.isEmpty(oMetadataElement.getMetadataId()))
            result = oMetadataElementService.selectOMetadataElementList(oMetadataElement);
        return getDataTable(result);
    }

    /**
     * 导出元数据要素列表
     */
    @RequiresPermissions("metadata:metadataElement:export")
    @Log(title = "元数据要素", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OMetadataElement oMetadataElement)
    {
        List<OMetadataElement> list = oMetadataElementService.selectOMetadataElementList(oMetadataElement);
        ExcelUtil<OMetadataElement> util = new ExcelUtil<OMetadataElement>(OMetadataElement.class);
        return util.exportExcel(list, "元数据要素数据");
    }

    /**
     * 新增元数据要素
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存元数据要素
     */
    @RequiresPermissions("metadata:metadataElement:add")
    @Log(title = "元数据要素", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OMetadataElement oMetadataElement)
    {
        return toAjax(oMetadataElementService.insertOMetadataElement(oMetadataElement));
    }

    /**
     * 修改元数据要素
     */
    @RequiresPermissions("metadata:metadataElement:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        OMetadataElement oMetadataElement = oMetadataElementService.selectOMetadataElementById(id);
        mmap.put("oMetadataElement", oMetadataElement);
        return prefix + "/edit";
    }

    /**
     * 修改保存元数据要素
     */
    @RequiresPermissions("metadata:metadataElement:edit")
    @Log(title = "元数据要素", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OMetadataElement oMetadataElement)
    {
        return toAjax(oMetadataElementService.updateOMetadataElement(oMetadataElement));
    }

    /**
     * 删除元数据要素
     */
    @RequiresPermissions("metadata:metadataElement:remove")
    @Log(title = "元数据要素", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(oMetadataElementService.deleteOMetadataElementByIds(ids));
    }
}
